
*** Settings ***
Library    Collections
Library    SerialLibrary
Library    String
Library    Process
Library    OperatingSystem
Library    DeviceLibrary.py     ${TARGET}       ${POWER}
Suite Setup       Device Init
Suite Teardown    Device DeInit
Test Setup        Power Reset
Test Teardown     Clear Env On Teardown

*** Variables ***
${TARGET}         linux
${POWER}       /dev/ttyUSB0
${LD_LIBRARY_PATH}    ./soc/lib

*** Keywords ***
########################  SERIAL  #########################
Set Environment
    Set Environment Variable    LD_LIBRARY_PATH    ${LD_LIBRARY_PATH}
    ${test_module}=    Evaluate    os.path.splitext(os.path.basename('${SUITE_SOURCE}'))[0]    modules=os
    Set Environment Variable    TEST_MODULE    ${test_module}
    Create Directory    logs

Device DeInit
    Log To Console    ============== robot test finish =====================
    Power Off

Device Init
    [Timeout]   5min
    Set Environment
    Update Firmware    ${SUITE_SOURCE}

########################  Teardown  #########################
Clear Env On Teardown
    Run Keyword And Ignore Error    Terminate All Processes  kill=True

##############################################################
*** Test Cases ***
{%- for case in case_list %}
{{case["NAME"]}}
    [tags]      {{case["TAG"]}} {{"\n"}}
    {%- for cmd in case["commands"] %}
    {%- set cnt = loop.index0 -%}
    {%- if cmd["ld_lib"] %}
    Set Environment Variable    LD_LIBRARY_PATH    {{cmd["ld_lib"]}}
    {%- else %}
    Set Environment Variable    LD_LIBRARY_PATH    ${LD_LIBRARY_PATH}
    {%- endif %}
    {%- if cmd["device"] %}
    ${process}=     Start Process   ./device.py    {{cmd["action"]}}    alias=cmd_{{cnt}}   stdout=logs/res_${TEST_NAME}_{{cnt}}.txt    stderr=logs/res_${TEST_NAME}_{{cnt}}.txt
    Log     ${process}      console=yes
    {%- if cmd["sleep"] %}
    Sleep   {{cmd["sleep"]}}
    {%- endif %}
    {%- else %}
    ${process}=     Start Process    {{cmd["action"]}}    alias=cmd_{{cnt}}   stdout=logs/res_${TEST_NAME}_{{cnt}}.txt   stderr=logs/res_${TEST_NAME}_{{cnt}}.txt
    Log     ${process}    console=yes
    {%- if cmd["sleep"] %}
    Sleep   {{cmd["sleep"]}}
    {% else %}
    Sleep   0.5
    {%- endif %}
    {%- endif %}
    {%- endfor %} {{"\n"}}
    {%- for cmd in case["commands"] %}
    {%- set cnt = loop.index0 -%}
    {%- if cmd["wait"] %}
        {%- if cmd["wait"] == True %}
    Wait For Process        cmd_{{cnt}}      timeout=2min
        {%- else %}
    Wait For Process        cmd_{{cnt}}      timeout={{cmd["wait"]}}min
        {%- endif %}
    {%- endif %}
    {%- endfor %} {{"\n"}}
    {%- for cmd in case["commands"] %}
    {%- set cnt = loop.index0 -%}
    {%- if not cmd["wait"] %}
    Terminate Process       cmd_{{cnt}}
    {%- endif %}
    {%- endfor %} {{"\n"}}
    {%- for cmd in case["commands"] %}
    {%- set cnt = loop.index0 %}
    ${res_{{cnt}}}=     Get File    logs/res_${TEST_NAME}_{{cnt}}.txt       encoding_errors=ignore
    Log    ${res_{{cnt}}}
    {%- endfor %} {{"\n"}}
    {%- for cmd in case["commands"] %}
    {%- set cnt = loop.index0 -%}
    {%- for op, pattern in cmd["assert"] %}
    {%- if op  == "~" %}
    Should Contain         ${res_{{cnt}}}      {{pattern}}
    {%- elif op  == "!~" %}
    Should Not Contain     ${res_{{cnt}}}      {{pattern}}
    {%- elif op  == "==" %}
    Should Be Equal        ${res_{{cnt}}}      {{pattern}}
    {%- elif op  == "!=" %}
    Should Be Not Equal    ${res_{{cnt}}}      {{pattern}}
    {%- endif %}
    {%- endfor %}
    {%- endfor %}
{%- endfor %}
